package org.hzz.application.handler;

import io.gitee.pkmer.designpattern.strategy.AbstractExecuteStrategy;
import lombok.Setter;
import org.hzz.application.command.GetCouponCmd;
import org.hzz.promotions.domain.entity.CouponEntity;
import org.hzz.promotions.domain.entity.CouponScopeEntity;
import org.hzz.promotions.domain.service.CouponDomainService;
import org.hzz.promotions.domain.service.CouponScopeDomainService;
import org.hzz.promotions.types.vo.CouponVo;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author <a href="mailto:1193094618@qq.com">pkmer</a>
 * <br>
 * <a href = "https://gitee.com/developeros/videos-online">Code Repository</a>
 * At 2024/9/1
 * 处理获取优惠券详情命令处理器
 */
@Component
public class GetCouponCmdHandler implements AbstractExecuteStrategy<GetCouponCmd, CouponVo> {

    @Setter(onMethod_ = @Autowired)
    private CouponDomainService couponDomainService;

    @Setter(onMethod_ = @Autowired)
    private CouponScopeDomainService scopeDomainService;
    @Override
    public String mark() {
        return GetCouponCmd.MARK;
    }

    @Override
    public CouponVo executeWithResp(GetCouponCmd cmd) {
        CouponEntity couponEntity = couponDomainService.getCouponEntity(cmd.getId());

        CouponVo couponVo = Converter.INSTANCE.toCouponVo(couponEntity);

        if(couponVo.getSpecific()){
            List<Long> scopes = scopeDomainService.getCouponScopeByCouponId(cmd.getId())
                    .stream()
                    .map(CouponScopeEntity::getBizId)
                    .toList();

            couponVo.setScopes(scopes);
        }

        return couponVo;
    }

    @Mapper
    interface Converter{
        Converter INSTANCE = Mappers.getMapper(Converter.class);
        CouponVo toCouponVo(CouponEntity entity);
    }
}
